package com.ygl.controller;

import com.ygl.entity.User;
import com.ygl.framework.jwt.JwtUtil;
import com.ygl.service.UserService;
import com.ygl.utils.Maps;
import com.ygl.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@Api(tags = "用户管理相关接口")
public class LoginController {

    @Autowired
    UserService userService;

    @ApiOperation("用户登录接口")
    @PostMapping("/login")
    public Result login(@RequestBody Map<String, String> map) {
        String userName = map.get("userName");
        String password = map.get("password");
        User user = userService.login(userName, password);
        if (user != null) {
            String token = JwtUtil.sign(user);
            Map <String ,Object>  login =  Maps.build().put("token",token).put("user",user).getMap();
            return Result.ok(login);
        } else {
            return Result.fail("用户名或密码错误");
        }

    }
}
